package com.spring.springboot.java;

import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.IdUtil;
import com.google.common.base.Joiner;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.spring.springboot.entity.Child;
import lombok.extern.log4j.Log4j2;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.security.SecureRandom;
import java.time.LocalDate;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 15625
 * @date 2020-12-04 11:17
 * @description: lambda表达式测试
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@Log4j2
public class LambdaDemo {

    private static final SecureRandom random = new SecureRandom();
    private static final List<Integer> list = new ArrayList<>(16);
    private static final List<Child> children = new ArrayList<>(16);

    @Test
    public void lambda() {
        log.info(Dict.create().set("startTime", LocalDate.now().minusDays(7)).set("endTime", LocalDate.now()).set("startTime", LocalDate.now().plusDays(7)));
        // 判断字符串是否为空串或者为null
        log.info(Strings.isNullOrEmpty(""));
        // 全时间格式
        ZonedDateTime zonedDateTime = ZonedDateTime.now();
        log.info("zonedDateTime:{}", zonedDateTime);
        log.info("zonedDateTime:{}", zonedDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        List<String> names = Arrays.asList("hangman", "lisa", "wangle", "zaria", "tianjin", "tom", "singsong", "kansan", "michael", "jain", "lucy", "anderson");
        /* 排序测试 */
        for (int i = 0; i < 5; i++) {
            int nextInt = random.nextInt(20) + 3;
            list.add(nextInt);
            int parameter = (int) (Math.random() * 10) + 1;
            Child child = new Child();
            child.setWid(IdUtil.fastSimpleUUID());
            child.setName(names.get(parameter));
            child.setAge(nextInt);
            children.add(child);
        }
        // 正序
        list.sort(Comparator.naturalOrder());
        log.info("list正序排列:{}", list);
        // 倒序
        list.sort(Comparator.reverseOrder());
        log.info("list倒序排列:{}", list);
        // 正序
        children.sort(Comparator.comparing(Child::getAge));
        log.info("用户集合根据年龄正序排列:{}", children);
        // 倒序
        children.sort(Comparator.comparing(Child::getAge).reversed());
        log.info("用户集合根据年龄倒序排列:{}", children);

    }

    @Test
    public void joiner() {
        // 将集合内的元素以","分隔,返回字符串。
        log.info(Joiner.on(",").join(Lists.newArrayList("tom", "singsong", "kansan")));
        // useForNull("value"),会以value代替集合中的null值。
        log.info(Joiner.on(",").useForNull("bayou").join(Lists.newArrayList("tom", "singsong", null, "kansan")));
        // skipNulls(),会忽略集合中的null值
        log.info(Joiner.on(",").skipNulls().join(Lists.newArrayList("tom", "singsong", null, "kansan")));

        List<String> list = Lists.newArrayList("张无忌", "周芷若", "赵敏", "张三丰", "殷梨亭", "俞岱岩", "莫声谷");

        String join = String.join(",", list);
        log.info(join);

        //filter   筛选的是要留下的，并非是筛选掉
        list = list.stream().filter(str -> str.startsWith("张")).collect(Collectors.toList());
        log.info("filter:{}", list);
    }

    @Test
    public void sort() {
        ArrayList<Integer> integers = Lists.newArrayList(1, 2, 3, 4, 5);

        integers.sort(Comparator.naturalOrder());
        log.info("integers顺序排列:{}", integers);

        integers.sort(Comparator.reverseOrder());
        log.info("integers倒序排列:{}", integers);

        List<Map<String, Integer>> lists = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Map<String, Integer> data = new HashMap<>();
            data.put("lili", i);
            lists.add(data);
        }
        //顺序排列
        lists.sort(Comparator.comparing(o -> o.get("lili")));
        log.info("lists顺序排列:{}", lists);

        //倒序排列
        lists.sort(Comparator.comparing(o -> o.get("lili")));
        log.info("lists倒序排列:{}", lists);

        lists.sort(Comparator.comparing(o -> o.get("cj")));

        lists.sort(Comparator.comparing(o -> o.get("cj")));
    }
}
